package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.dto.MyParam;
import com.gxa.dto.ResultDto;
import com.gxa.mapper.UserMapper;
import com.gxa.pojo.User;
import com.gxa.service.UserService;
import com.gxa.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据条件查找用户
     * @param user
     * @return
     */
    @Override
    public ResultDto findUserByParam(User user, MyParam myParam) {
        PageHelper.startPage(myParam.getPage(), myParam.getLimit());
        List<User> userList = userMapper.findUserByParam(user);
        PageInfo<User> userPageInfo = new PageInfo<>(userList);
        return Response.success(0, "查找成功！", userList, Integer.valueOf(userPageInfo.getTotal()+""));
    }

    /**
     * 删除用户
     * @param ids
     * @return
     */
    @Override
    public ResultDto delte(List<String> ids) {
        int i = userMapper.delete(ids);
        if (i<=0){
            return Response.error(1001, "删除失败！");
        }
        return Response.success("删除成功！");
    }

    /**
     * 根据id查找用户
     * @param userId
     * @return
     */
    @Override
    public User findByPK(Integer userId) {
        return userMapper.selectByPrimaryKey(userId );
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public ResultDto add(User user) {
        int insert = userMapper.insert(user);
        if (insert<=0){
            return Response.error(1001, "添加失败！");
        }
        return Response.success("添加成功！");
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @Override
    public Integer update(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 根据电话号码查找用户
     * @param userPhone
     * @return
     */
    @Override
    public User findByPhone(String userPhone) {
        return userMapper.findUserByPhone(userPhone);
    }
}
